﻿using AnimatedGif;
using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.TextUtils;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PixivGifDownLoad.Helper
{
    internal class CreateGif
    {
        public static string GetId(String url)
        {
            String ArtID = "";

            if (TextHelper.IsIntNumber(url))
                return url;
            else
            {
                ArtID = TextHelper.ReplaceTextEnhance(url, "https://www.pixiv.net/artworks/", "");
                if (TextHelper.IsIntNumber(ArtID))
                    return ArtID;
                else
                    return "";//如果依然不是数字则代表出问题了
            }

            //https://www.pixiv.net/member_illust.php?mode=medium&illust_id=66477035
            //https://touch.pixiv.net/member_illust.php?mode=medium&illust_id=
        }

        /// <summary>
        /// 封装下载单个文件的类
        /// </summary>
        /// <param name="url">下载文件的URL</param>
        /// <param name="FolderPath">放置文件的文件夹路径，如C://</param>
        /// <param name="RetryCount">重试次数，默认5</param>
        /// <returns></returns>
        public static async Task<String> LoadFromZipAsync(string Filename, int Time, GifQuality quality = GifQuality.Bit8)
        {
            return await Task.Run(() =>
            {
                return LoadFromZip(Filename, Time, quality);
            });
        }

        /// <summary>
        /// 直接从zip文件读取图片并且压缩，只支持一级目录
        /// </summary>
        /// <param name="Filename"></param>
        /// <param name="quality"></param>
        public static string LoadFromZip(string Filename, int Time, GifQuality quality = GifQuality.Bit8)
        {
            using (FileStream zipToOpen = new FileStream(Filename, FileMode.Open))
            {
                using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Read))
                {
                    String FilePath = PixivStaticValue.GifPath1 + string.Format("{0}.gif", Path.GetFileNameWithoutExtension(Filename));
                    Console.WriteLine(FilePath);

                    using (AnimatedGifCreator gifCreator = new AnimatedGifCreator(FilePath, Time))
                    {
                        PixivStaticValue.JpgMaxCount1 = archive.Entries.Count;
                        foreach (ZipArchiveEntry readmeEntry in archive.Entries)
                        {
                            PixivStaticValue.JpgCount1++;
                            using (Image img = Image.FromStream(readmeEntry.Open()))
                            {
                                Console.WriteLine("插入算法：" + quality + DateTime.Now);
                                gifCreator.AddFrame(img, -1, quality);
                            } //Image disposed
                        }
                    }
                }
            }
            PixivStaticValue.JpgLock1 = true;
            try { File.Delete(Filename); }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            return Filename;
        }

        /// <summary>
        /// 根据用户选择返回对应的分类算法
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static GifQuality GetQuality(String text)
        {
            GifQuality quality = GifQuality.Bit8;
            if (text == "高保真")
                quality = GifQuality.Bit8;

            if (text == "高压缩")
                quality = GifQuality.Bit4;

            if (text == "放大")
                quality = GifQuality.Default;
            if (text == "灰度")
                quality = GifQuality.Grayscale;
            return quality;
        }
    }
}